<template>
  <div class="manage">
    <el-dialog :title="新增公司" :visible.sync="isShow">
      <common-form :formLabel="operateFormLabel" :form="operateForm" ref="form"></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="add" align="right">+ 添加</el-button>
      <common-form inline :formLabel="formLabel" :form="searchFrom" v-if="showClose">
        <el-button type="primary" @click="getList(searchFrom.keyword)">搜索</el-button>
      </common-form>
    </div>
    <!--依次是: 表格数据 表格标签数据 分页数据  列表方法 更新方法 删除方法-->
    <common-table :tableData="tableData" :tableLabel="tableLabel" :config="config" @changePage="getList()"
                  @del="del"
    ></common-table>
  </div>
</template>

<script>
import CommonForm from '../../components/CommonForm'
import CommonTable from '../../components/CommonTable'

export default {
  components: {
    CommonForm,
    CommonTable
  },
  data() {
    return {
      operateType: 'add',
      isShow: false,
      tableData: [],
      tableLabel: [
        {
          prop: 'type',
          label: '公司'
        }
      ],
      config: {
        page: 1,
        total: 30,
        loading: false,
        showCtl: true
      },
      operateForm: {
        type: ''
      },
      operateFormLabel: [
        {
          model: 'type',
          label: '公司',
          type: 'input'
        }
      ],
      searchFrom: {
        keyword: ''
      },
      formLabel: [
        {
          model: 'keyword',
          label: '',
          type: 'input'
        }
      ]
    }
  },
  methods: {
    getList(name = '') {
      this.config.loading = true
      this.$http
        .get('/mark/getDivisionNameList')
        .then(res => {
          this.tableData = res.data.map(item => {
            return {
              'type': item.DivisionName
            }
          })
          this.config.total = res.data.count
          this.config.loading = false
        })
    },
    add() {
      this.operateForm = {}
      this.isShow = true
    },
    confirm() {
      this.$http.post('/mark/addDivisionName', this.operateForm).then(res => {
        console.log(res.data)
        this.isShow = false
        if (res.data === 'RES_SQLITE_CONSTRAINT') {
          this.$message('所属公司已存在')
        } else {
          this.getList()
        }
      })
    },
    del(row) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const type = row.type
          console.log('type:' + type)
          this.$http
            .get('/mark/delDivisionName', {
              params: {
                type
              }
            })
            .then(res => {
              console.log(res.data)
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getList()
            })
        })
        .catch((err) => {
          console.log(err)
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
@import 'src/styles/scss/common';
</style>
